import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: 'login' */ '@/views/Login.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import(/* webpackChunkName: "register" */ '../views/Register.vue')
  },
  {
    path: '/',
    name: 'home',
    component: () => import(/* webpackChunkName: "home" */ '@/views/Home.vue'),
    children: [
      {
        path: '/',
        component: () => import(/* webpackChunkName: "home" */ '@/components/home')
      },
      {
        path: 'departments',//组织架构
        component: () => import(/* webpackChunkName: "home" */ '@/components/departments')
      },
      {
        path: 'employees',//员工
        component: () => import(/* webpackChunkName: "home" */ '@/components/employees/index.vue'),
      },
      {
        path: 'employees/edit',
        component: () => import(/* webpackChunkName: "home" */ '@/components/employees/edit.vue')
      },{
        path: 'approvals',//审批列表
        component: () => import(/* webpackChunkName: "home" */ '@/components/approval')
      },
    ]
  },
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // console.log(to);
  let token = sessionStorage.getItem('token')
  if (to.path == '/login' || to.path == '/register' || token) {
    next()
    return
  }
  next('/login')
})
export default router
